package com.cy.pj.sys.controller;

import com.cy.pj.common.pojo.JsonResult;
import com.cy.pj.sys.pojo.SysRole;
import com.cy.pj.sys.pojo.SysUser;
import com.cy.pj.sys.pojo.SysUserDept;
import com.cy.pj.sys.service.SysUserService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.Map;

/**
 * @author 张琪
 * @date 2020/11/17
 * @describe
 */

@RequestMapping("/user/")
@RestController
public class SysUserController {

    @Autowired
    private SysUserService sysUserService;

    @RequestMapping("doFindPageObjects")
    /* 方法1:
    public JsonResult doFindObjects(String username, Integer pageCurrent){
        return new JsonResult(sysUserService.findPageObjects(username, pageCurrent));
    }*/
    public JsonResult doFindObjects1(String username, Integer pageCurrent){
        return new JsonResult(sysUserService.findPageObjects1(username, pageCurrent));
    }

    @RequestMapping("doSaveObject")
    public JsonResult doFindObjectById(SysUser sysUser, Integer[] roleIds){
        System.out.println(sysUser);
        int rows = sysUserService.saveObject(sysUser, roleIds);
        if(rows == 0)
            return new JsonResult("insert fault");
        return new JsonResult("insert success");
    }

    @RequestMapping("doValidById")
    public JsonResult doValidById(Integer id, Integer valid){
        sysUserService.validById(id, valid);
        return new JsonResult("update OK");
    }

    @RequestMapping("doFindObjectById")
    public JsonResult doFindById(Integer id){
        System.out.println(id);
        Map<String, Object> map = sysUserService.doFindById(id);
        return new JsonResult(map);
    }

    @RequestMapping("doUpdateObject")
    public JsonResult doUpdateObject(SysUser sysUser, Integer[] roleIds){
        System.out.println(sysUser);
        int rows = sysUserService.updateObject(sysUser, roleIds);
        if(rows == 0)
            return new JsonResult("update fault");
        return new JsonResult("update success");
    }

    @RequestMapping("doLogin")
    public JsonResult doLogin(boolean isRememberMe, String username, String password){
        Subject subject = SecurityUtils.getSubject();
        UsernamePasswordToken upToken =
                new UsernamePasswordToken(
                        username,
                        password
                );
        System.out.println(111);
        if(isRememberMe){
            System.out.println(222);
            upToken.setRememberMe(true);
        }
        subject.login(upToken);

        return new JsonResult("login ok");
    }

    @RequestMapping("doUpdatePassword")
    public JsonResult doUpdatePassword(String pwd, String newPwd, String cfgPwd){
        sysUserService.updatePassword(pwd, newPwd, cfgPwd);
        return new JsonResult("update ok");

    }
}
